<template>
  <el-col class="app-v-center app-column" style="overflow-y: auto;">
    <el-row class="app-h-48px app-fill-width app-v-center pl-4 app-bb-1px-l2" style="background: #dfe9f5" :gutter="10">
      <el-button
        size="mini"
        :disabled="role>2"
        @click="goAddActivity"
      >创建活动</el-button>
      <div class="app-flex-1" />
      <el-col v-if="role===1" :span="6" class="app-h-end">
        <el-input v-model="name" class="mr-3" placeholder="请输入用户名" />
        <el-button type="primary" @click="getActivities">查询</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="activityList"
      class="app-flex-1"
      style="width: 100%;overflow-y: auto;"
    >
      <el-table-column
        prop="name"
        label="活动名称"
        align="center"
        width="180"
      />
      <!-- <el-table-column
        prop="activeDesc"
        align="center"
        label="活动描述"
      /> -->
      <el-table-column
        prop="sponsorshipFees"
        align="center"
        label="活动赞助费"
      />
      <el-table-column
        prop="activeCost"
        align="center"
        label="活动总成本"
      />
      <el-table-column
        prop="endTime"
        align="center"
        label="结束时间"
      />
      <el-table-column
        prop="username"
        align="center"
        label="创建人"
      />
      <el-table-column
        align="center"
        width="300"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="copyLink(scope.row)"
          >复制地址</el-button>
          <el-button
            size="mini"
            @click="goManage(scope.index,scope.row)"
          >管理</el-button>
          <el-button
            v-if="role < 3"
            size="mini"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            v-if="role < 3"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mb-10 mt-5"
      background
      :current-page.sync="pageNo"
      :page-sizes="[10, 20, 50]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="getUserList(pageNo,pageSize)"
      @current-change="getUserList(pageNo,pageSize)"
    />
    <el-dialog
      :title="isEdit? '修改活动' : '创建活动'"
      :visible.sync="addActivityDialog"
      width="50vw"
    >
      <el-form ref="form" :rules="rules" class="app-width-80p" :model="form" label-width="160px">
        <el-form-item prop="name" label="活动名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item prop="activeDesc" label="活动描述">
          <el-input v-model="form.activeDesc" />
        </el-form-item>
        <el-form-item prop="sponsorshipFees" label="活动赞助费">
          <el-input v-model.number="form.sponsorshipFees" type="number" />
        </el-form-item>
        <el-form-item prop="endTime" label="结束时间">
          <el-date-picker
            v-model="form.endTime"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            @change="getVal"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addActivityDialog = false">取 消</el-button>
        <el-button type="primary" @click="addActivity">确 定</el-button>
      </div>
    </el-dialog>
  </el-col>
</template>

<script>
import { updateActivity,addActivity,deleteActivity,getActivities } from '@/api/activity'
export default {
  data: () => ({
    rules: {
      name: [
        { required: true, message: '请输入活动名称', trigger: 'blur' }
      ],
      activeDesc: [
        { required: true, message: '请输入活动描述', trigger: 'blur' }
      ],
      sponsorshipFees: [
        { required: true, message: '请输入活动奖品成本', trigger: 'blur' }
      ],
      endTime: [
        { required: true, message: '请选择结束时间', trigger: 'blur' }
      ]
    },
    isEdit: false,
    addActivityDialog: false,
    role: Number(JSON.parse(localStorage.getItem('userInfo')).role),
    name: '',
    activityList: [{
      name: '样例',
      count: 2,
      cost: 500,
      createTime: '2023-04-05'
    }],
    pageNo: 1,
    pageSize: 10,
    total: 0,
    form: {
      name: '',
      activeDesc: '',
      sponsorshipFees: '',
      endTime: ''
    }
  }),
  mounted() {
    this.getActivities()
  },
  methods: {
    getVal(e) {
      console.log(e)
    },
    // 打开添加弹窗
    goAddActivity() {
      this.form = {
        name: '',
        activeDesc: '',
        sponsorshipFees: ''
      }
      this.isEdit = false
      this.addActivityDialog = true
    },
    addActivity() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            updateActivity(this.form).then(res => {
              if (res.code === 200) {
                this.getActivities()
                this.$message({ type: 'success',message: '操作成功' })
                this.addActivityDialog = false
              }
            })
          } else {
            addActivity(this.form).then(res => {
              if (res.code === 200) {
                this.pageNo = 1
                this.getActivities()
                this.$message({ type: 'success',message: '操作成功' })
                this.addActivityDialog = false
              }
            })
          }
        }
      })
    },
    getActivities() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        username: this.name
      }
      getActivities(params).then(res => {
        if (res.code === 200) {
          this.activityList = res.data.dataList
          this.total = res.data.total
        }
      })
    },
    copyLink(row) {
      const link = `${location.origin}/#/index-mobile?code=${row.id}`
      navigator.clipboard.writeText(link).then(() => {
        this.$message({ type: 'success',message: '复制成功' })
      })
    },
    goManage(index,row) {
      this.$router.push({ name: 'dispatch-manage',query: { id: row.id }})
    },
    handleEdit(row) {
      this.isEdit = true
      this.form = row
      this.addActivityDialog = true
    },
    handleDelete(row) {
      this.$confirm(`确认删除活动${row.name}？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteActivity({ id: row.id }).then(res => {
          if (res.data.code === 200) {
            this.$message({ type: 'success',message: '已删除' })
            this.pageNo = 1
            this.getActivities()
          }
        })
      }).catch(() => {
        this.$message('已取消')
      })
    }
  }
}
</script>
